// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    addHoldMJTile(mjTile) {
        this.m_holdMjTiles.push(mjTile);
        return mjTile;
    },

    // 添加一张已出牌
    addOutMJTile(mjTile) {
        if (this.m_outMjTiles == null)
            return;

        this.m_outMjTiles.push(mjTile);
    },

    // 添加一组杠
    addBarMJTile(_isBrightBar, barData) {
        if (_isBrightBar) {
            this.m_mjTileBrightBars.push(barData);
        } else {
            this.m_mjTileDarkBars.push(barData);
        }
    },

    // 添加一组补张
    addBuMJTile(_isBrightBu, buData) {
        if (_isBrightBu) {
            this.m_mjTileBrightBu.push(barData);
        } else {
            this.m_mjTileDarkBu.push(barData);
        }
    },

    // 添加一组碰牌
    addPungMJTile(pungData) {
        this.m_mjTilePungs.push(pungData);
    },

    // 通过已有数据添加一组明杠
    addBrightBar(_data) {
        this.m_mjTileBrightBars.push(_data);
    },

    getDarkBars() {
        if (this.m_fixedDarkBar) {
            return this.m_fixDarkBars;
        }
        else {
            return this.m_mjTileDarkBars ? this.m_mjTileDarkBars : [];
        }
    },

    getBrightBars() {
        return this.m_mjTileBrightBars ? this.m_mjTileBrightBars : [];
    },

    getBrightBus() {
        return this.m_mjTileBrightBu ? this.m_mjTileBrightBu : [];
    },

    getDrakBus() {
        return this.m_mjTileDarkBu ? this.m_mjTileDarkBu : [];
    },

    getPungs() {
        return this.m_mjTilePungs ? this.m_mjTilePungs : [];
    },

    getEatMJTiles() {
        return this.m_mjTileEat ? this.m_mjTileEat : [];
    },

    // 对手中牌进行排序
    sortHoldMJTiles() {
        if (this.m_holdMjTiles == null || this.m_holdMjTiles.length < 1) {
            return;
        }

        function getPRI(mjTile) {
            var color = mjTile.mjColor;
            var num = mjTile.mjNumber;
            var pro = mjTile.mjProperty;
            var pri = color * 10 + num;
            if (gt.getMjProperty(pro, 1) == 1) {
                pri = pri + 0;
            } else {
                pri = pri + 1000;
            }
            return pri;
        }

        // 排序手牌
        this.m_holdMjTiles.sort(function (a, b) {
            return getPRI(a) < getPRI(b);
        });
    },

    // 对手中含有金牌进行排序
    sortHoldMJTilesByJin() {
        if (this.m_holdMjTiles == null || this.m_holdMjTiles.length < 1) {
            return;
        }

        function getPRI(mjTile) {
            var color = mjTile.mjColor;
            var num = mjTile.mjNumber;
            var pro = mjTile.mjProperty;
            var pri = color * 10 + num;
            if (gt.getMjProperty(pro, 5) == 1) {
                pri = pri + 0;
            } else {
                pri = pri + 1000;
            }
            return pri;
        }

        // 排序手牌
        this.m_holdMjTiles.sort(function (a, b) {
            return getPRI(a) < getPRI(b);
        });
    },

    // 更据却牌类型对手中牌进行排序
    sortHoldMJTilesByQue(nQueType) {
        if (this.m_holdMjTiles == null || this.m_holdMjTiles.length < 1) {
            return;
        }

        function getPRI(mjTile) {
            var color = mjTile.mjColor;
            var num = mjTile.mjNumber;
            var pro = mjTile.mjProperty;
            var pri = color * 10 + num;
            if (nQueType && nQueType == color) {
                pri = pri + 1000;
            }
            return pri;
        }

        // 排序手牌
        this.m_holdMjTiles.sort(function (a, b) {
            return getPRI(a) < getPRI(b);
        });
    },

    // 排序亮牌
    sortHoldLLMjTiles() {
        if (this.m_holdMjTiles == null || this.m_holdMjTiles.length < 1) {
            return;
        }

        function getPRI(mjTile) {
            var color = mjTile.mjColor;
            var num = mjTile.mjNumber;
            var pro = mjTile.mjProperty;
            var pri = color * 10 + num;
            if (gt.getMjProperty(pro, 1) == 1) {
                // 癞子
                pri = pri + 0;
            }
            else if (gt.getMjProperty(pro, 2) == 1) {
                // 亮牌
                pri = pri - 500;
            } else {
                // 其他普通牌
                pri = pri + 1000;
            }
            if (gt.getMjProperty(pro, 3) == 1) {
                // 扣听那张牌
                pri = pri - 1000;
            }
            return pri;
        }

        // 排序手牌
        this.m_holdMjTiles.sort(function (a, b) {
            return getPRI(a) < getPRI(b);
        });

        // 亮牌抽离排序
        var tbLightCards = [];
        for (var i = this.m_holdMjTiles.length; i >= 0; i--) {
            var v = this.m_holdMjTiles[i];
            if (gt.getMjProperty(v.mjProperty, 2) == 1) {
                tbLightCards.push(v);
                this.m_holdMjTiles.splice(i, 1);
            }
        }
        // 排序亮牌
        tbLightCards.sort(function (a, b) {
            return getPRI(a) < getPRI(b);
        });

        // 重新插入亮牌到手牌
        for (var i = tbLightCards.length; i >= 0; i--) {
            this.m_holdMjTiles.splice(i, 1, tbLightCards[i]);
        }
    },

    // 玩家持有牌
    setHoldMJTiles(_holdMJTiles) {
        this.m_holdMjTiles = _holdMJTiles;
    },

    getHoldMJTiles() {
        return this.m_holdMjTiles ? this.m_holdMjTiles : [];
    },

    cleanHoldMJTiles() {
        if (!this.m_holdMjTiles) {
            return;
        }

        for (var o in this.m_holdMjTiles) {
            mjTile.mjTileSpr.stopAllActions();
            mjTile.mjTileSpr.removeFromParent();
        }
        this.m_holdMjTiles = [];
    },

    getHoldTileByIdx(_idx) {
        return this.m_holdMjTiles[_idx];
    },

    // 根据颜色和数字在碰牌列表中移除碰牌
    removePungsTile(_mjColor, _mjNumber) {
        for (var mjTile in this.m_mjTilePungs) {
            if (mjTile.mjColor == _mjColor && mjTile.mjNumber == _mjNumber) {
                this.m_mjTilePungs.splice(i, 1);
                return mjTile;
            }
        }
    },

    // 从持有牌中移除特定数量的牌
    removeHoldTileByCount(_count, _mjColor, _mjNumber) {
        var already = 0;
        var i = 1;
        while (i <= this.m_holdMjTiles.length) {
            var mjTile = this.m_holdMjTiles[i];
            if (already < _count && mjTile.mjColor == _mjColor && mjTile.mjNumber == _mjNumber) {
                mjTile.mjTileSpr.removeFromParent();
                this.m_holdMjTiles.splice(i, 1);
                this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
                already = already + 1;
            } else {
                i = i + 1;
            }

            if (already >= _count) {
                return true;
            }
        }
    },

    // 从持有牌中移除特定数量的亮牌A
    removeHoldLLTileByCountA(_count, _mjColor, _mjNumber) {
        var nCurRemoveCnt = 0;
        for (i = this.m_holdMjTiles.length; i >= 0; i--) {
            var mjTile = this.m_holdMjTiles[i]
            if (gt.getMjProperty(mjTile.mjProperty, 2) == 1 && mjTile.mjColor == _mjColor && mjTile.mjNumber == _mjNumber) {
                mjTile.mjTileSpr.removeFromParent();
                this.m_holdMjTiles.splice(i, 1);
                this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
                nCurRemoveCnt = nCurRemoveCnt + 1;
                if (nCurRemoveCnt >= _count) {
                    break;
                }
            }
        }
        return nCurRemoveCnt;
    },

    // 从持有牌中移除特定数量的亮牌B
    removeHoldLLTileByCountB(_count, _mjColor, _mjNumber, nCurRemoveCnt) {
        for (i = this.m_holdMjTiles.length; i >= 0; i--) {
            if (nCurRemoveCnt >= _count) {
                break;
            }
            var mjTile = this.m_holdMjTiles[i];
            if (mjTile.mjColor == _mjColor && mjTile.mjNumber == _mjNumber) {
                mjTile.mjTileSpr.removeFromParent()
                this.m_holdMjTiles.splice(i, 1);
                this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
                nCurRemoveCnt = nCurRemoveCnt + 1;
            }
        }
        return nCurRemoveCnt;
    },

    // 根据索引,从持有牌中移除此牌
    removeHoldTileByIdx(_idx) {
        var mjTile = this.m_holdMjTiles[_idx];
        if (mjTile) {
            mjTile.mjTileSpr.removeFromParent();
            this.m_holdMjTiles.splice(_idx, 1);
            this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
        }
    },

    // 根据牌，从持有牌中移除此牌
    removeHoldTileByMJ(_mjColor, _mjNumber, _mjProperty) {
        for (var index = this.m_holdMjTiles.length; i >= 0; i--) {
            var mjTile = this.m_holdMjTiles[index]
            if (mjTile.mjColor == _mjColor && mjTile.mjNumber == _mjNumber && mjTile.mjProperty == _mjProperty) {
                mjTile.mjTileSpr.removeFromParent()
                this.m_holdMjTiles.splice(index, 1);
                this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
                return true;
            }
        }
        return false;
    },

    removeHoldTileLast() {
        var mjTile = this.m_holdMjTiles[this.m_holdTilesTotal];
        if (mjTile) {
            mjTile.mjTileSpr.removeFromParent();
            this.m_holdMjTiles.splice(this.m_holdTilesTotal, 1);
            this.m_holdTilesTotal = this.m_holdTilesTotal - 1;
        }
    },

    // 玩家已出牌
    getOutMJTiles() {
        return this.m_outMjTiles ? this.m_outMjTiles : [];
    },

    getOutTileByIdx(_idx) {
        return this.m_outMjTiles[_idx];
    },

    removeOutTileByIdx(_idx) {
        var mjTile = this.m_outMjTiles[_idx];
        if (mjTile) {
            mjTile.mjTileSpr.removeFromParent()
            this.m_outMjTiles.splice(_idx, 1);
        }
    },

    removeOutTileLast(cover) {
        var mjTile = this.m_outMjTiles[this.m_outMjTiles.length - 1];
        if (mjTile) {
            if (cover) {
                mjTile.mjTileSpr.setColor(cc.c3b(160, 160, 160));
            } else {
                mjTile.mjTileSpr.removeFromParent();
                this.m_outMjTiles.pop();
            }
        }
    },

    // 设置麻将位置参考点
    setTilesReferPos(_tilesReferPos) {
        this.m_tilesReferPos = _tilesReferPos;
    },

    getTilesReferPos() {
        return this.m_tilesReferPos;
    },

    // 玩家持有牌数量
    setHoldTilesTotal(_holdTilesTotal) {
        this.m_holdTilesTotal = _holdTilesTotal;
    },

    getHoldTilesTotal() {
        return this.m_holdTilesTotal;
    },

    // 唯一标识
    setuid(_uid) {
        this.m_uid = _uid;
    },

    getuid() {
        return this.m_uid;
    },

    // ip
    setIP(_ip) {
        this.m_ip = _ip;
    },

    getIP(_ip) {
        return this.m_ip;
    },

    // 昵称
    setName(_name) {
        this.m_name = _name;
    },

    getName() {
        return this.m_name;
    },

    // 头像下载地址
    setHeadURL(_headURL) {
        this.m_headURL = _headURL;
    },

    getHeadURL() {
        return this.m_headURL;
    },

    // 性别
    setSex(_sex) {
        this.m_sex = _sex
    },

    getSex() {
        return this.m_sex;
    },

    // 座位标识(服务器m_pos + 1获得)
    setSeatIdx(_seatIdx) {
        this.m_seatIdx = _seatIdx;
    },

    getSeatIdx() {
        return this.m_seatIdx;
    },

    // 实际显示位置
    setDisplayIdx(_displayIdx) {
        this.m_displayIdx = _displayIdx;
    },

    getDisplayIdx() {
        return this.m_displayIdx;
    },

    // 准备状态
    setReady(_ready) {
        this.m_ready = _ready;
    },

    getReady() {
        return this.m_ready;
    },

    // 分数
    setScore(_score) {
        this.m_score = _score;
        if (this.m_scoreLabel && this.m_score) {
            this.m_scoreLabel.setString(tostring(this.m_score));
        }
    },

    // 设置头像框
    setHeadFrame(nheadFrame) {
        this.m_headFrame = nheadFrame;
    },

    // 获取头像框
    getHeadFrame() {
        return this.m_headFrame;
    },

    getScore() {
        return this.m_score;
    },

    // 分数label
    setScoreLabel(_scoreLabel) {
        this.m_scoreLabel = _scoreLabel;
    },

    getScoreLabel() {
        return this.m_scoreLabel;
    },

    // 是否为庄家
    setBanker(_banker) {
        this.m_isBanker = _banker;
    },

    getBanker() {
        return this.m_isBanker;
    },

    // 是否杠过牌
    setGang(_gang) {
        this.m_gang = _gang;
    },

    getGang() {
        return this.m_gang;
    },

    // 设置坐标
    setLocation(_location) {
        this.m_location = _location;
    },

    getLocation() {
        return this.m_location ? this.m_location : '';
    },

    getCheckCradNum() {
        if ((this.m_holdMjTiles.length) % 3 == 1) {
            return true;
        }
        // gt.log("err hold mjtiles:"..#this.m_holdMjTiles)
        // if (buglyReportLuaException) {
        //     if (gt.playerData && gt.playerData.uid) {
        //         buglyAddUserValue("resVersion", tostring(cc.exports.gt.resVersion) or "")
        //         buglySetUserId(tostring(gt.playerData.uid))
        //     }
        //     buglyReportLuaException(("error hold tiles num"), debug.traceback())
        // }
        return false;
    },

    // 处理特殊情况判断丢牌
    // nFixNum 当前数量+nFixNum
    getCheckCradNumFix(nFixNum) {
        if ((this.m_holdMjTiles.length + nFixNum) % 3 == 1) {
            return true;
        }
        // gt.log("err hold mjtiles:"..#this.m_holdMjTiles)
        // if (buglyReportLuaException) {
        //     if (gt.playerData && gt.playerData.uid) {
        //         buglyAddUserValue("resVersion", tostring(cc.exports.gt.resVersion) or "")
        //         buglySetUserId(tostring(gt.playerData.uid))
        //     }
        //     buglyReportLuaException(("error hold tiles num"), debug.traceback())
        // }
        return false;
    },
    start() {

    },

    // update (dt) {},
});
